package com.yztc.orangeentertainment.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.facebook.drawee.view.SimpleDraweeView;
import com.yztc.orangeentertainment.R;
import com.yztc.orangeentertainment.base.OnItemClickListener;
import com.yztc.orangeentertainment.bean.staritembean.WishListBean;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by My on 2016/10/8.
 * 留言板adapter
 */
public class StarWishAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private int TYPE1 = 1;
    private int TYPE2 = 2;
    private Context context;

    private List<WishListBean> list;

    private OnItemClickListener listener;

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public void setList(List<WishListBean> list) {
        this.list = list;
    }

    public StarWishAdapter(Context context) {
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 1) {
            return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_star_wish, null));
        } else {
            return new MyViewHolder2(LayoutInflater.from(context).inflate(R.layout.item_star_wish_copy, null));
        }
    }


    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof MyViewHolder) {
            ((MyViewHolder) holder).ivWishPortrait.setImageURI(list.get(position).getUsr().getPortrait());
            ((MyViewHolder) holder).tvWishName.setText(list.get(position).getUsr().getName());
            ((MyViewHolder) holder).tvWishAddTime.setText(list.get(position).getAddTime());
            ((MyViewHolder) holder).tvWishContent.setText(list.get(position).getContent());
                Glide.with(context).load(list.get(position).getImg()).placeholder(R.mipmap.ic_pic_loading_default).error(R.mipmap.ic_pic_loading_default).diskCacheStrategy(DiskCacheStrategy.ALL).into( ((MyViewHolder) holder).ivWishPic);

            ((MyViewHolder) holder).tvWishZan.setText(list.get(position).getPraise() + context.getResources().getString(R.string.zan));
            ((MyViewHolder) holder).relWish.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((MyViewHolder) holder).tvWishZan.setText((list.get(position).getPraise() + 1) + context.getResources().getString(R.string.zan));
                    ((MyViewHolder) holder).ivWishDigg.setImageResource(R.mipmap.ic_comment_digg_selected);
                }
            });
            if (! ((MyViewHolder) holder).tvWishZan.getText().equals(list.get(position).getPraise() + context.getResources().getString(R.string.zan))) {
                Toast.makeText(context, context.getResources().getString(R.string.yizan), Toast.LENGTH_SHORT).show();
            }
        }
        if (holder instanceof  MyViewHolder2){
                    ((MyViewHolder2) holder).ivWishPortrait2.setImageURI(list.get(position).getUsr().getPortrait());
            ((MyViewHolder2) holder).tvWishName2.setText(list.get(position).getUsr().getName());
            ((MyViewHolder2) holder).tvWishAddTime2.setText(list.get(position).getAddTime());
            ((MyViewHolder2) holder).tvWishContent2.setText(list.get(position).getContent());

            ((MyViewHolder2) holder).tvWishZan2.setText(list.get(position).getPraise() + context.getResources().getString(R.string.zan));
            ((MyViewHolder2) holder).relWish2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((MyViewHolder2) holder).tvWishZan2.setText((list.get(position).getPraise() + 1) + context.getResources().getString(R.string.zan));
                    ((MyViewHolder2) holder).ivWishDigg2.setImageResource(R.mipmap.ic_comment_digg_selected);
                }
            });
            if (! ((MyViewHolder2) holder).tvWishZan2.getText().equals(list.get(position).getPraise() + context.getResources().getString(R.string.zan))) {
                Toast.makeText(context, context.getResources().getString(R.string.yizan), Toast.LENGTH_SHORT).show();
            }
        }
        if (listener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    listener.onItemClick(holder.itemView, pos);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = holder.getLayoutPosition();
                    listener.onItemLongClick(holder.itemView, pos);
                    return false;
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (!list.get(position).getImg().equals("") && list.get(position).getImg() != null) {
            return TYPE1;
        } else {
            return TYPE2;
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @InjectView(R.id.iv_wish_portrait)
        SimpleDraweeView ivWishPortrait;
        @InjectView(R.id.tv_wish_name)
        TextView tvWishName;
        @InjectView(R.id.tv_wish_add_time)
        TextView tvWishAddTime;
        @InjectView(R.id.iv_wish_pic)
        ImageView ivWishPic;
        @InjectView(R.id.tv_wish_content)
        TextView tvWishContent;
        @InjectView(R.id.iv_wish_digg)
        ImageView ivWishDigg;
        @InjectView(R.id.tv_wish_zan)
        TextView tvWishZan;
        @InjectView(R.id.rel_wish)
        RelativeLayout relWish;
        @InjectView(R.id.rel_wish_img)
        RelativeLayout relWishImg;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.inject(this, itemView);
        }
    }


    class MyViewHolder2 extends RecyclerView.ViewHolder {
        @InjectView(R.id.iv_wish_portrait2)
        SimpleDraweeView ivWishPortrait2;
        @InjectView(R.id.tv_wish_name2)
        TextView tvWishName2;
        @InjectView(R.id.tv_wish_add_time2)
        TextView tvWishAddTime2;
        @InjectView(R.id.tv_wish_content2)
        TextView tvWishContent2;
        @InjectView(R.id.iv_wish_digg2)
        ImageView ivWishDigg2;
        @InjectView(R.id.tv_wish_zan2)
        TextView tvWishZan2;
        @InjectView(R.id.rel_wish2)
        RelativeLayout relWish2;
        public MyViewHolder2(View itemView) {
            super(itemView);
            ButterKnife.inject(this, itemView);
        }
    }



}
